﻿Imports System.IO
Imports EHaskins.Frc.Dashboard.FrcPacketUtilities

Public Class RobotPacket

    Private _isValid As Boolean
    Private _replyId As UShort
    Private _robotMac As Byte()
    Private _teamNumber As Integer
    Dim _batteryVoltage As Double

    Public Sub New()

    End Sub
    Public Sub New(ByVal data As Byte())
        Me.Parse(data)
    End Sub

    Public Sub Parse(ByVal data As Byte())
        Dim stream As New MemoryStream(data)
        Dim reader As New BinaryReader(stream)

        reader.ReadByte()
        Dim batteryBytes = reader.ReadBytes(2)
        BatteryVoltage = Convert.ToDecimal(batteryBytes(0).ToString("x")) + (Convert.ToDecimal(batteryBytes(1).ToString("x")) / 100.0)

        reader.ReadBytes(5)

        TeamNumber = reader.ReadTeamNumber()
        RobotMac = reader.ReadBytes(6)

        reader.ReadBytes(14)

        ReplyId = reader.EReadUInt16()

        reader.ReadBytes(Configuration.UserControlDataSize + 4)

        IsValid = VerifyFrcCrc(data)
    End Sub

    Public Property BatteryVoltage() As Double
        Get
            Return _batteryVoltage
        End Get
        Set(ByVal value As Double)
            _batteryVoltage = value
        End Set
    End Property
    Public Property IsValid() As Boolean
        Get
            Return _isValid
        End Get
        Set(ByVal value As Boolean)
            _isValid = value
        End Set
    End Property
    Public Property ReplyId() As Integer
        Get
            Return _replyId
        End Get
        Set(ByVal value As Integer)
            If _replyId = value Then
                Return
            End If
            _replyId = value
        End Set
    End Property
    Public Property RobotMac() As Byte()
        Get
            Return _robotMac
        End Get
        Set(ByVal value As Byte())
            _robotMac = value
        End Set
    End Property
    Public Property TeamNumber() As Integer
        Get
            Return _teamNumber
        End Get
        Set(ByVal value As Integer)
            If _teamNumber = value Then
                Return
            End If
            _teamNumber = value
        End Set
    End Property
End Class
